package com.ruoyi.media.sphxdSend.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.helper.LoginHelper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.media.sphxdSend.domain.bo.MediaSphxdSendBo;
import com.ruoyi.media.sphxdSend.domain.vo.MediaSphxdSendVo;
import com.ruoyi.media.sphxdSend.domain.MediaSphxdSend;
import com.ruoyi.media.sphxdSend.mapper.MediaSphxdSendMapper;
import com.ruoyi.media.sphxdSend.service.IMediaSphxdSendService;

import java.util.*;

/**
 * 视频号小店一键发货Service业务层处理
 *
 * @author dqw
 * @date 2023-06-04
 */
@RequiredArgsConstructor
@Service
public class MediaSphxdSendServiceImpl extends ServiceImpl<MediaSphxdSendMapper,MediaSphxdSend> implements IMediaSphxdSendService {

    private final MediaSphxdSendMapper baseMapper;

    /**
     * 查询视频号小店一键发货
     */
    @Override
    public MediaSphxdSendVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询视频号小店一键发货列表
     */
    @Override
    public TableDataInfo<MediaSphxdSendVo> queryPageList(MediaSphxdSendBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<MediaSphxdSend> lqw = buildQueryWrapper(bo);
        Page<MediaSphxdSendVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询视频号小店一键发货列表
     */
    @Override
    public List<MediaSphxdSendVo> queryList(MediaSphxdSendBo bo) {
        LambdaQueryWrapper<MediaSphxdSend> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<MediaSphxdSend> buildQueryWrapper(MediaSphxdSendBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<MediaSphxdSend> lqw = Wrappers.lambdaQuery();
        LoginUser loginUser =   LoginHelper.getLoginUser();
        lqw.eq(MediaSphxdSend::getUserId,loginUser.getUserId());
        lqw.eq(bo.getDeptId() != null, MediaSphxdSend::getDeptId, bo.getDeptId());
        lqw.eq(bo.getUserId() != null, MediaSphxdSend::getUserId, bo.getUserId());
        lqw.eq(StringUtils.isNotBlank(bo.getAccountId()), MediaSphxdSend::getAccountId, bo.getAccountId());
        lqw.eq(StringUtils.isNotBlank(bo.getOrderId()), MediaSphxdSend::getOrderId, bo.getOrderId());
        lqw.like(StringUtils.isNotBlank(bo.getNickName()), MediaSphxdSend::getNickName, bo.getNickName());
        lqw.eq(StringUtils.isNotBlank(bo.getDescTitle()), MediaSphxdSend::getDescTitle, bo.getDescTitle());
        lqw.like(StringUtils.isNotBlank(bo.getProvinceName()), MediaSphxdSend::getProvinceName, bo.getProvinceName());
        lqw.like(StringUtils.isNotBlank(bo.getCityName()), MediaSphxdSend::getCityName, bo.getCityName());
        lqw.like(StringUtils.isNotBlank(bo.getCountyName()), MediaSphxdSend::getCountyName, bo.getCountyName());
        lqw.eq(StringUtils.isNotBlank(bo.getDetailInfo()), MediaSphxdSend::getDetailInfo, bo.getDetailInfo());
        lqw.eq(StringUtils.isNotBlank(bo.getPostalCode()), MediaSphxdSend::getPostalCode, bo.getPostalCode());
        lqw.eq(StringUtils.isNotBlank(bo.getProductPrice()), MediaSphxdSend::getProductPrice, bo.getProductPrice());
        lqw.eq(StringUtils.isNotBlank(bo.getOrderPrice()), MediaSphxdSend::getOrderPrice, bo.getOrderPrice());
        lqw.eq(StringUtils.isNotBlank(bo.getOrderStatus()), MediaSphxdSend::getOrderStatus, bo.getOrderStatus());
        return lqw;
    }

    /**
     * 新增视频号小店一键发货
     */
    @Override
    public Boolean insertByBo(MediaSphxdSendBo bo) {
        MediaSphxdSend add = BeanUtil.toBean(bo, MediaSphxdSend.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改视频号小店一键发货
     */
    @Override
    public Boolean updateByBo(MediaSphxdSendBo bo) {
        MediaSphxdSend update = BeanUtil.toBean(bo, MediaSphxdSend.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(MediaSphxdSend entity){
        //TODO 做一些数据校验,如唯一约束
        LoginUser loginUser =  LoginHelper.getLoginUser();
        entity.setUserId(loginUser.getUserId());
        entity.setDeptId(loginUser.getDeptId());
    }


    /**
     * 批量删除视频号小店一键发货
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
